package com.hundsun.cprs.yyt.web.action.report;


import com.hundsun.cprs.yyt.service.report.domain.DaliyClinchSum;
import com.hundsun.cprs.yyt.service.report.domain.DaliyClinchSumVo;
import com.hundsun.cprs.yyt.service.trade.dto.TradeMatchDetailInfo;
import com.hundsun.cprs.yyt.util.ExportExcelUtil;
import com.hundsun.cprs.yyt.web.util.ExportExcelWrapper;
import com.hundsun.cprs.yyt.web.vo.excel.TradeMatchDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hundsun.cprs.yyt.common.enums.EnumAreaCode;
import com.hundsun.cprs.yyt.service.report.domain.query.DaliyClinchSumQuery;
import com.hundsun.cprs.yyt.service.report.service.DaliyClinchSumService;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.web.action.BaseAction;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("report/")
public class ReportAction extends BaseAction {

    @Autowired
    private DaliyClinchSumService daliyClinchSumService;

    @RequestMapping("retailArea/clinch/list.htm")
    public void retailAreaList(@ModelAttribute("query")DaliyClinchSumQuery query,ModelMap model){
        query.setAreaCode(EnumAreaCode.RETAIL_AREA.getCode());
        chansformDate(query);
        daliyClinchSumService.queryByPage(query);
        model.addAttribute("EnumAreaCodeMap",EnumAreaCode.toMap());
    }

    @RequestMapping("wholesaleArea/clinch/list.htm")
    public void wholesaleAreaList(@ModelAttribute("query")DaliyClinchSumQuery query,ModelMap model){
        query.setAreaCode(EnumAreaCode.WHOLESALE_AREA.getCode());
        chansformDate(query);
        daliyClinchSumService.queryByPage(query);
        model.addAttribute("EnumAreaCodeMap",EnumAreaCode.toMap());
    }

    protected void chansformDate(DaliyClinchSumQuery query){
        if (query.getStatTimeStart() != null){
            query.setStatTimeStart(DateUtil.getBeginOfDay(query.getStatTimeStart()));
        }
        if (query.getStatTimeEnd() != null){
            query.setStatTimeEnd(DateUtil.getEndOfDay(query.getStatTimeEnd()));
        }
    }

    @RequestMapping("retailArea/clinch/export.htm")
    public void exportRetailList(DaliyClinchSumQuery query, HttpServletResponse response){
        query.setPageSize(30000);
        chansformDate(query);
        query.setAreaCode(EnumAreaCode.RETAIL_AREA.getCode());
        daliyClinchSumService.queryByPage(query);

        List<DaliyClinchSumVo> exportResult = new ArrayList<DaliyClinchSumVo>();
        for(DaliyClinchSum item : query.getData()){
            exportResult.add(new DaliyClinchSumVo(item));
        }

        String[] columnNames = { "所属区域","成交金额汇总（元）","成交数量汇总（件）","统计时间"};
        String fileName = "零售区日成交统计列表";
        ExportExcelWrapper<DaliyClinchSumVo> util = new ExportExcelWrapper<DaliyClinchSumVo>();
        util.exportExcel(fileName, fileName, columnNames, exportResult, response, ExportExcelUtil.EXCEl_FILE_2007);
    }

    @RequestMapping("wholesaleArea/clinch/export.htm")
    public void exportWholesaleAreaList(DaliyClinchSumQuery query, HttpServletResponse response){
        query.setPageSize(30000);
        chansformDate(query);
        query.setAreaCode(EnumAreaCode.WHOLESALE_AREA.getCode());
        daliyClinchSumService.queryByPage(query);

        List<DaliyClinchSumVo> exportResult = new ArrayList<DaliyClinchSumVo>();
        for(DaliyClinchSum item : query.getData()){
            exportResult.add(new DaliyClinchSumVo(item));
        }

        String[] columnNames = { "所属区域","成交金额汇总（元）","成交数量汇总（件）","统计时间"};
        String fileName = "批发区日成交统计报表";
        ExportExcelWrapper<DaliyClinchSumVo> util = new ExportExcelWrapper<DaliyClinchSumVo>();
        util.exportExcel(fileName, fileName, columnNames, exportResult, response, ExportExcelUtil.EXCEl_FILE_2007);

    }
}
